﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Data;
using System.Xml;
using System.Web.UI.HtmlControls;
using System.IO;
namespace SERP.Modules
{
    public class ModuleBase : UserControl
    {

        public static readonly string BaseUrl = System.Configuration.ConfigurationSettings.AppSettings["MAIN_URL"];
        public static readonly string ImagesUrl = System.Configuration.ConfigurationSettings.AppSettings["IMAGE_URL"];
        public static readonly string IconUrl = System.Configuration.ConfigurationSettings.AppSettings["ICON_URL"];

        public string GetModuleCode()
        {
            if (Request["Module"] != null)
            {
                return Request["Module"].Trim();
            }
            else
            {
                return "";
            }
        }
        public string SplipString(string text)
        {
            string content = "";
            if (text.Length > 30)
            {
                content = text.Substring(0, 30);
                content = content + "...";
            }
            else
                content = text;
            return content;
        }
        public void RedirectToPage(string PageName)
        {
            RedirectToPage("", PageName);
        }
        public void RedirectToPage(string SubModule, string PageName)
        {
            RedirectToPage("", SubModule, PageName, "");
        }
        public void RedirectToPage(string Module, string SubModule, string PageName)
        {
            RedirectToPage(Module, SubModule, PageName, "");
        }
        public string GetRedirectUrl(string SubModule, string PageName)
        {
            return GetRedirectUrl("", SubModule, PageName, "");
        }
        public string GetRedirectUrl(string Module, string SubModule, string PageName)
        {
            return GetRedirectUrl(Module, SubModule, PageName, "", false);
        }
        public string GetRedirectUrl(string Module, string SubModule, string PageName, string Parameters)
        {
            return GetRedirectUrl(Module, SubModule, PageName, Parameters, false);
        }

        public string GetRedirectUrl(string Module, string SubModule, string PageName, bool Resolve)
        {
            return GetRedirectUrl(Module, SubModule, PageName, "", Resolve);
        }
        public string GetRedirectUrl(string mod, string smod, string page, string Parameters, bool Resolve)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            if (Request["Module"] != null)
                dic.Add("module", Request["module"]);
            if (Request["pmid"] != null)
                dic.Add("pmid", Request["pmid"]);
            if (mod.Trim().Length > 0)
            {
                dic.Add("mod", mod);
            }
            else
            {
                if (Request["mod"] != null)
                {
                    dic.Add("mod", Request["mod"]);
                }
            }
            if (smod.Trim().Length > 0)
            {
                dic.Add("smod", smod);
            }
            else
            {
                if (Request["smod"] != null)
                {
                    dic.Add("smod", Request["smod"]);
                }
            }
            
            if (page.Trim().Length > 0)
            {
                dic.Add("page", page);
            }
            else
            {
                if (Request["page"] != null)
                {
                    dic.Add("page", Request["page"]);
                }
            }
            List<string> ls = new List<string>();
            foreach (string st in dic.Keys)
            {
                ls.Add(st + "=" + dic[st]);
            }
            string stURL = BaseUrl + "?" + string.Join("&", ls.ToArray()) + (Parameters.Length > 0 ? "&" + Parameters : "");
            if (!Resolve)
            {
                return stURL;
            }
            return ResolveUrl(stURL);
        }
        public void RedirectToPage(string Module, string SubModule, string PageName, string Parameters)
        {
            Response.Redirect(GetRedirectUrl(Module, SubModule, PageName, Parameters), true);
        }

        public void ShowPopUp(string ModuleName, string SubModuleName, string PageName, string Parameters)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ShowPopUp", "<script>showPopUpPage('" + ModuleName + "','" + SubModuleName + "','" + PageName + "','" + Parameters + "');</script>");
        }
        public void ShowPopUp(string ModuleName, string SubModuleName, string PageName, string Parameters, string width, string height)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ShowPopUp", "<script>showPopUp('" + ModuleName + "','" + SubModuleName + "','" + PageName + "','" + Parameters + "','" + width + "','" + height + "');</script>");
        }
        public int CurrentServicePoint
        {
            get
            {
                return 0;
            }
        }
        protected override void OnLoad(EventArgs e)
        {

            base.OnLoad(e);
        }


        public static string GetUserFilesStore()
        {
            return GetRootDirFileStores() + "/" + HttpContext.Current.User.Identity.Name;
        }
        public static string GetRootDirFileStores()
        {
            string st = "~/Files";
            if (System.Configuration.ConfigurationSettings.AppSettings["FILE_STORE_PATH"] != null)
            {
                st = System.Configuration.ConfigurationSettings.AppSettings["FILE_STORE_PATH"];
            }
            st = st.TrimEnd('/');
            return st;
        }
    }

   
}